<template>
  <div>
    <el-container class="header">
      <!-- 头 -->
      <el-header class="father">
        <el-form :model="form" label-width="100px">
          <el-row :gutter="20" style="height: 58px">
            <el-col :span="15"
              ><div style="font-size: 12px; color: #ffc0cb">
                说明：目前支持学科和关键字条件筛选
              </div></el-col
            >
            <el-col :span="6"><div>&nbsp;</div> </el-col>
            <el-col :span="1"
              ><el-button
                class="added"
                type="success"
                icon="el-icon-edit"
                @click="$router.push('new')"
                >新增试题</el-button
              ></el-col
            >
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="学科">
                <el-select v-model="form.subjectID" placeholder="请选择">
                  <el-option
                    :label="item.subjectID"
                    :value="item.subjectID"
                    v-for="item in list"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录"
                ><el-select placeholder="请选择" value=""> </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签"
                ><el-select placeholder="请选择" value=""> </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键词">
                <el-input
                  v-model="form.keyword"
                  placeholder="根据题干搜索"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="试题类型">
                <el-select v-model="form.questionType" placeholder="请选择">
                  <el-option label="单选" :value="1"></el-option>
                  <el-option label="多选" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度"
                ><el-select v-model="form.difficulty" placeholder="请选择">
                  <el-option label="简单" :value="1"></el-option>
                  <el-option label="一般" :value="2"></el-option>
                  <el-option label="困难" :value="3"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向"
                ><el-select v-model="form.direction" placeholder="请选择">
                  <el-option
                    :label="item.direction"
                    :value="item.direction"
                    v-for="item in list"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人">
                <el-select v-model="form.creatorID" placeholder="请选择">
                  <el-option
                    :label="item.creator"
                    :value="item.creator"
                    v-for="item in list"
                    :key="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="题目备注">
                <el-input v-model="form.remarks"></el-input> </el-form-item
            ></el-col>
            <el-col :span="6">
              <el-form-item label="企业简称">
                <el-input v-model="form.shortName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="城市">
                <el-select
                  v-model="form.province"
                  @keyup.enter="handleFilter"
                  @change="handleProvince"
                  filterable
                  placeholder="请选择"
                  style="width: 100px"
                >
                  <el-option
                    v-for="item in citySelect.province"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
                <el-select
                  v-model="form.city"
                  @keyup.enter="handleFilter"
                  filterable
                  placeholder="请选择"
                  style="width: 100px"
                >
                  <el-option
                    v-for="item in citySelect.cityDate"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5"
              ><div>
                <el-button class="confirm" @click="empty">清空</el-button>
                <el-button type="primary" class="cancel" @click="onSubmit"
                  >搜索</el-button
                >
              </div></el-col
            >
          </el-row>
        </el-form>
      </el-header>

      <!-- 身体 -->
      <el-main>
        <!-- 数据条数 -->
        <div class="recording">
          <span
            ><i class="el-icon-info"> </i>数据一共
            {{ tableData.counts }} 条</span
          >
        </div>
        <el-table :data="list" v-loading="loading">
          <el-table-column prop="number" align="center" label="试题编号" />
          <el-table-column prop="subject" align="center" label="学科" />
          <el-table-column prop="catalog" align="center" label="目录" />
          <el-table-column prop="questionType" align="center" label="题型">
            <template v-slot="{ row }">
              <span>
                {{
                  row.questionType === "1"
                    ? "单选"
                    : row.questionType === "2"
                    ? "多选"
                    : "无"
                }}
              </span>
            </template></el-table-column
          >
          <el-table-column prop="question" align="center" label="题干">
            <template v-slot="{ row }">
              <span v-html="row.question"></span> </template
          ></el-table-column>
          <el-table-column
            width="180"
            prop="addDate"
            align="center"
            label="录入时间"
          >
            <template v-slot="{ row }">
              <span>
                {{
                  row.addDate.substring(0, 10) +
                  " " +
                  row.addDate.substring(11, 19)
                }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="difficulty" align="center" label="难度">
            <template v-slot="{ row }">
              <span>
                {{
                  row.difficulty === "1"
                    ? "简单"
                    : row.questionType === "2"
                    ? "一般"
                    : row.questionType === "3"
                    ? "困难"
                    : "无"
                }}</span
              >
            </template></el-table-column
          >
          <el-table-column prop="creator" align="center" label="录入人" />
          <el-table-column align="center" width="200" label="操作">
            <!-- 表格子项的作用域插槽 -->
            <template #default="{ row }">
              <div>
                <el-button
                  type="primary"
                  icon="el-icon-view"
                  plain
                  circle
                  @click="openDrawer('view', row.id)"
                ></el-button>
                <el-button
                  type="success"
                  icon="el-icon-edit"
                  plain
                  circle
                  @click="openDrawer('edit', row.id)"
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  plain
                  circle
                  @click="del(row.id)"
                ></el-button>
                <el-button
                  type="warning"
                  icon="el-icon-check"
                  plain
                  circle
                  @click="openDrawer('join', row.id)"
                ></el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件区域 -->
        <el-row type="flex" style="height: 60px" align="middle" justify="end">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryObj.page"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="queryObj.pagesize"
            layout="prev, pager, next, sizes, jumper"
            :total="tableData.counts"
          >
          </el-pagination>
        </el-row>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/questions.js'
import { provinces, citys } from '@/api/hmmm/citys.js'

export default {
  data () {
    return {
      loading: false,
      form: {
        subjectID: null,
        difficulty: null,
        questionType: null,
        province: null,
        city: null,
        keyword: null,
        remarks: null,
        shortName: null,
        direction: null,
        creatorID: null
      },
      tableData: {},
      list: [],
      queryObj: {
        page: 1, // 控制当前页
        pagesize: 10 // 控制每页有几条数据
      },
      citySelect: {
        province: '',
        cityDate: ''
      }
    }
  },
  created () {
    this.initTable()
    this.getCityData()
  },
  methods: {
    // 渲染
    async initTable () {
      this.loading = true
      const { data } = await list({ ...this.queryObj, ...this.form })
      this.tableData = data
      this.list = data.items
      this.loading = false
    },
    // 搜索
    onSubmit () {
      this.initTable()
    },
    // 清空
    empty () {
      this.form = {
        subjectID: null,
        difficulty: null,
        questionType: null,
        province: null,
        city: null,
        keyword: null,
        remarks: null,
        shortName: null,
        direction: null,
        creatorID: null
      }
    },
    // 删除
    del (id) {
      remove({ id })
      this.initTable()
      this.$message.success('删除成功')
    },
    // TODO
    openDrawer (type) {
      if (type === 'edit') {
        this.$router.push('new')
      }
    },
    // 分页功能
    // pagesize改变时会触发
    handleSizeChange (val) {
      this.queryObj.pagesize = val
      this.queryObj.page = 1
      this.initTable()
    },
    // page当前页改变时会触发
    async handleCurrentChange (val) {
      // 改变参数对象中的当前页
      this.queryObj.page = val
      this.initTable()
    },
    // 获取省
    getCityData: function () {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    handleProvince: function (e) {
      this.citySelect.cityDate = citys(e)
    }
  }
}
</script>

<style scoped lang="less">
.header {
  background-color: #fff;
  margin: 10px 20px;
  border-radius: 5px;
  box-shadow: 0px 0px 20px #ccc;
  .father {
    padding: 20px 20px;
    margin-bottom: 170px;
    .added {
      height: 32px;
      width: 96px;
      font-size: 12px;
      padding-left: 11px;
    }
  }
}
.recording {
  height: 35px;
  width: 100%;
  padding: 8px 16px;
  background-color: #f4f4f5;
  border-radius: 5px;
  span {
    font-size: 13px;
    color: #909399;
  }
}
</style>
